package com.ccs.transmittal;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.Select;

import com.ccs.testcase.Transmittal_Details;
import com.ccs.testcase.Transmittal_ToWebApp;

public class Misc_Transmittal {

	static
	Admin_Transmittal_List atl = new Admin_Transmittal_List();
	
	public void go_to_transmittal_app(WebDriver driver){
		driver.findElement(By.xpath("//div[@id='mainContent')]/div[4]/nav/ul/li[3]")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
	}
	public void go_to_transmittal_admin(WebDriver driver){
		driver.findElement(By.xpath("//a[contains(.,'View Transmittal')]")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
	}
	public void go_to_admin_home(WebDriver driver){
		driver.findElement(By.xpath("//a[contains(.,'Administration')]")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
	}
	public void go_to_create_transmittal(WebDriver driver){
		driver.findElement(By.xpath("//button[contains(.,'Create Transmittal')]")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
	}
	public void maximize_window(WebDriver driver){
		driver.manage().window().maximize();
	}
	public String random_number(){
		double num =  Math.floor((Math.random()*9999)+1);
		String code = String.valueOf(num);
		code = code.replaceAll("\\.","");
		return code;
	}
	public void verify_table_header_inpatient_hospital(WebDriver driver){
		
		String tableHeader[] = { "Visit Number", "SOA", "Admission Date", "Discharge Date", "Patient Name", "Hospital Bill(Php)", "LOA Number", "Action" };
		
		String vn = driver.findElement(By.xpath("//div[@id='collapseOne']/div/div/table/thead/tr/th[2]")).getText();
		String soa = driver.findElement(By.xpath("//div[@id='collapseOne']/div/div/table/thead/tr/th[3]")).getText();
		String ad = driver.findElement(By.xpath("//div[@id='collapseOne']/div/div/table/thead/tr/th[4]")).getText();
		String dd = driver.findElement(By.xpath("//div[@id='collapseOne']/div/div/table/thead/tr/th[5]")).getText();
		String pn = driver.findElement(By.xpath("//div[@id='collapseOne']/div/div/table/thead/tr/th[6]")).getText();
		String hb = driver.findElement(By.xpath("//div[@id='collapseOne']/div/div/table/thead/tr/th[7]")).getText();
		String ln = driver.findElement(By.xpath("//div[@id='collapseOne']/div/div/table/thead/tr/th[8]")).getText();
		String a = driver.findElement(By.xpath("//div[@id='collapseOne']/div/div/table/thead/tr/th[9]")).getText();
		
		String tableText[] = { vn, soa, ad, dd, pn, hb, ln, a };
		
		for(int x=0; x<tableText.length ;x++){
			if(!tableHeader[x].equals(tableText[x])){
				System.out.println(tableHeader[x] +" is not equals: " +tableText[x]);
			}
		}
	}	
	public void verify_table_header_outpatient_hospital(WebDriver driver){
		
		String tableHeader[] = { "Visit Number", "SOA/CI", "Availment Date", "Patient Name", "Hospital Bill(Php)", "LOA Number", "Action" };
		
		String vn = driver.findElement(By.xpath("//div[@id='collapseTwo']/div/div/table/thead/tr/th[2]")).getText();
		String soa = driver.findElement(By.xpath("//div[@id='collapseTwo']/div/div/table/thead/tr/th[3]")).getText();
		String ad = driver.findElement(By.xpath("//div[@id='collapseTwo']/div/div/table/thead/tr/th[4]")).getText();
		String pn = driver.findElement(By.xpath("//div[@id='collapseTwo']/div/div/table/thead/tr/th[5]")).getText();
		String hb = driver.findElement(By.xpath("//div[@id='collapseTwo']/div/div/table/thead/tr/th[6]")).getText();
		String ln = driver.findElement(By.xpath("//div[@id='collapseTwo']/div/div/table/thead/tr/th[7]")).getText();
		String a = driver.findElement(By.xpath("//div[@id='collapseTwo']/div/div/table/thead/tr/th[8]")).getText();
		
		String tableText[] = { vn, soa, ad, pn, hb, ln , a };
		
		for(int x=0; x<tableText.length ;x++){
			if(!tableHeader[x].equals(tableText[x])){
				System.out.println(tableHeader[x] +" is not equals: " +tableText[x]);
			}
		}
	}
	public void verify_table_header_inpatient_professional_fee(WebDriver driver){
		
		String tableHeader[] = { "Visit Number", "Admission Date", "Discharge Date", "SOA" , "Doctor Name", "Patient Name", "PF Amount(Php)" };
		
		String vn = driver.findElement(By.xpath("//div[@id='collapseOnes']/div/div/table/thead/tr/th[1]")).getText();
		String ad = driver.findElement(By.xpath("//div[@id='collapseOnes']/div/div/table/thead/tr/th[2]")).getText();
		String dd = driver.findElement(By.xpath("//div[@id='collapseOnes']/div/div/table/thead/tr/th[3]")).getText();
		String soa = driver.findElement(By.xpath("//div[@id='collapseOnes']/div/div/table/thead/tr/th[4]")).getText();
		String dn = driver.findElement(By.xpath("//div[@id='collapseOnes']/div/div/table/thead/tr/th[5]")).getText();
		String pn = driver.findElement(By.xpath("//div[@id='collapseOnes']/div/div/table/thead/tr/th[6]")).getText();
		String pa = driver.findElement(By.xpath("//div[@id='collapseOnes']/div/div/table/thead/tr/th[7]")).getText();
		
		String tableText[] = { vn, ad, dd, soa, dn, pn, pa };
		
		for(int x=0; x<tableText.length ;x++){
			if(!tableHeader[x].equals(tableText[x])){
				System.out.println(tableHeader[x] +" is not equals: " +tableText[x]);
			}
		}
	}
	public void verify_table_header_outpatient_professional_fee(WebDriver driver){
		
		String tableHeader[] = { "Visit Number", "Availment Date", "SOA/CI", "Doctor Name", "Patient Name", "PF Amount(Php)" };
		
		String vn = driver.findElement(By.xpath("//div[@id='collapseTwos']/div/div/table/thead/tr/th[1]")).getText();
		String ad = driver.findElement(By.xpath("//div[@id='collapseTwos']/div/div/table/thead/tr/th[2]")).getText();
		String soa = driver.findElement(By.xpath("//div[@id='collapseTwos']/div/div/table/thead/tr/th[3]")).getText();
		String dn = driver.findElement(By.xpath("//div[@id='collapseTwos']/div/div/table/thead/tr/th[4]")).getText();
		String pn = driver.findElement(By.xpath("//div[@id='collapseTwos']/div/div/table/thead/tr/th[5]")).getText();
		String pa = driver.findElement(By.xpath("//div[@id='collapseTwos']/div/div/table/thead/tr/th[6]")).getText();
		
		String tableText[] = { vn, ad, soa, dn, pn, pa };
		
		for(int x=0; x<tableText.length ;x++){
			if(!tableHeader[x].equals(tableText[x])){
				System.out.println(tableHeader[x] +" is not equals: " +tableText[x]);
			}
		}
	}
	public void verify_table_inpatient_alphabetical(WebDriver driver){
		
		String sort = driver.findElement(By.xpath("//div[@id='collapseOne']/div/div/table/thead/tr/th[6]")).getAttribute("class");
		if(sort.equals("sorted asc")){
			System.out.println("Inpatient is sorted alphabetically. CORRECT!");
		}else{
			System.out.println("Inpatient is not sorted alphabetically. INCORRECT!");
		}
	}	
	public void verify_table_outpatient_alphabetical(WebDriver driver){
		
		String sort = driver.findElement(By.xpath("//div[@id='collapseTwo']/div/div/table/thead/tr/th[5]")).getAttribute("class");
		if(sort.equals("sorted asc")){
			System.out.println("Outpatient is sorted alphabetically. CORRECT!");
		}else{
			System.out.println("Outpatient is not sorted alphabetically. INCORRECT!");
		}
	}
	public void verify_location_transmittal_list(WebDriver driver){
		
		String loc = driver.findElement(By.xpath("//form[@id='transmittalForm']/ul/li/input[6]")).getAttribute("value");
		if(loc.equals("GLOBAL CITY")){
			System.out.println("Value of location: "+loc+" .CORRECT");
		}else{System.out.println("Value of location: "+loc+" .INCORRECT");}
	}
	public void verify_location_create_transmittal(WebDriver driver){
		
		String loc = driver.findElement(By.xpath("//div[@id='createFormDiv']/div[1]/input")).getAttribute("value");
		if(loc.equals("GLOBAL CITY")){
			System.out.println("Value of location: "+loc+" .CORRECT");
		}else{System.out.println("Value of location: "+loc+" .INCORRECT");}
	}
	public void verify_location_edit_transmittal(WebDriver driver){
		
		String loc = driver.findElement(By.xpath("//div[@id='editFormDiv']/span[2]")).getText();
		if(loc.equals("GLOBAL CITY")){
			System.out.println("Value of location: "+loc+" .CORRECT");
		}else{System.out.println("Value of location: "+loc+" .INCORRECT");}
	}
	public String[] get_table_details_inpatient_hospital(WebDriver driver, int x, String transmittal_type){
		
		int z=0;
		if(transmittal_type.equals("final")){
			z=1;
		}else{
			z=2;
		}
		
		String vn = driver.findElement(By.xpath("//div[@id='collapseOne']/div/div/table/tbody/tr["+x+"]/td["+z+"]")).getText();
		z++;
		String soa = driver.findElement(By.xpath("//div[@id='collapseOne']/div/div/table/tbody/tr["+x+"]/td["+z+"]")).getText();
		z++;
		String ad = driver.findElement(By.xpath("//div[@id='collapseOne']/div/div/table/tbody/tr["+x+"]/td["+z+"]")).getText();
		z++;
		String dd = driver.findElement(By.xpath("//div[@id='collapseOne']/div/div/table/tbody/tr["+x+"]/td["+z+"]")).getText();
		z++;
		String pn = driver.findElement(By.xpath("//div[@id='collapseOne']/div/div/table/tbody/tr["+x+"]/td["+z+"]")).getText();
		z++;
		String hb = driver.findElement(By.xpath("//div[@id='collapseOne']/div/div/table/tbody/tr["+x+"]/td["+z+"]")).getText();
		z++;
		String ln = driver.findElement(By.xpath("//div[@id='collapseOne']/div/div/table/tbody/tr["+x+"]/td["+z+"]")).getText();

		String tableText[] = { vn, soa, ad, dd, pn, hb, ln };
		
		return tableText;
		
	}
	public String[] get_table_details_outpatient_hospital(WebDriver driver, int x, String transmittal_type){
		
		//x serves as indicator for rows to return the following: DASOSS, POS, SPU --> Assign it to separate arrays

		int z=0;
		if(transmittal_type.equals("final")){
			z=1;
		}else{
			z=2;
		}
		
		String vn = driver.findElement(By.xpath("//div[@id='collapseTwo']/div/div/table/tbody/tr["+x+"]/td["+z+"]")).getText();
		z++;
		String soa = driver.findElement(By.xpath("//div[@id='collapseTwo']/div/div/table/tbody/tr["+x+"]/td["+z+"]")).getText();
		z++;
		String ad = driver.findElement(By.xpath("//div[@id='collapseTwo']/div/div/table/tbody/tr["+x+"]/td["+z+"]")).getText();
		z++;
		String pn = driver.findElement(By.xpath("//div[@id='collapseTwo']/div/div/table/tbody/tr["+x+"]/td["+z+"]")).getText();
		z++;
		String hb = driver.findElement(By.xpath("//div[@id='collapseTwo']/div/div/table/tbody/tr["+x+"]/td["+z+"]")).getText();
		z++;
		String ln = driver.findElement(By.xpath("//div[@id='collapseTwo']/div/div/table/tbody/tr["+x+"]/td["+z+"]")).getText();
		
		String tableText[] = { vn, soa, ad, pn, hb, ln };
		
		return tableText;
		
	}
	public void verify_transmittal_date_of_create_transmittal(WebDriver driver){
		//Verify if transmittal date is equals to current date
		String myDate = atl.date();
		String new_loa_date[] = myDate.split("/"); 

		String finalDate = new_loa_date[0]+"/"+new_loa_date[1]+"/"+new_loa_date[2];
		String adminDate = driver.findElement(By.xpath("//span[@id='spanSystemDate']")).getText();
		//System.out.println(finalDate);
		//System.out.println(adminDate);
		if(finalDate.equals(adminDate)){
			System.out.println("Transmittal Date is equals to current Date. CORRECT");
		}else{
			System.out.println("Transmittal Date is not equals to current Date. INCORRECT");
		}
		
	}
	public static String getHMO(String hmo){
		String setHmo="";
		String hmos[] = {

			"EASTWEST HEALTHCARE",
			"COCOLIFE HEALTH CARE"
		};
		for(int x=0;x<hmos.length;x++){
			String y[] = hmos[x].split(" "); 
			if(y[0].toLowerCase().toString().equals(hmo)){
				setHmo=hmos[x];
			}
		}
		return setHmo;
	}
	public void verify_hmo_transmittal(WebDriver driver, String hmos, String transmittalNo){
		
		String hmo = driver.findElement(By.xpath("//div[@id='editFormDiv']/span[1]")).getText();
		
		String getHMO = getHMO(hmos);
		System.out.println(getHMO);
		if(hmo.equals(getHMO)){
			System.out.println("Value of HMO: "+hmo+" .CORRECT");
		}
		else{  System.out.println("Value of HMO: "+hmo+" .INCORRECT"); }
		
		String transmittal_no = driver.findElement(By.xpath("//input[@id='transNo1']")).getAttribute("value");
		if(transmittalNo.equals(transmittal_no)){
			System.out.println("Value of Transmittal Number: "+transmittal_no+" .CORRECT");
		}else{  System.out.println("Value of Transmittal Number: "+transmittal_no+" .INCORRECT"); }
		
		
	}
	public void verify_hmo_transmittal_final(WebDriver driver, String hmos, String transmittalNo){
		
		String hmo = driver.findElement(By.xpath("//div[@id='editFormDiv']/span[1]")).getText();
		
		String getHMO = getHMO(hmos);
		System.out.println(getHMO);
		if(hmo.equals(getHMO)){
			System.out.println("Value of HMO: "+hmo+" .CORRECT");
		}
		else{  System.out.println("Value of HMO: "+hmo+" .INCORRECT"); }
		
		String transmittal_no = driver.findElement(By.xpath("//div[@id='editFormDiv']/span[3]")).getText();
		if(transmittalNo.equals(transmittal_no)){
			System.out.println("Value of Transmittal Number: "+transmittal_no+" .CORRECT");
		}else{  System.out.println("Value of Transmittal Number: "+transmittal_no+" .INCORRECT"); }
		
		
	}
	public String save_as_draft(WebDriver driver){
		driver.findElement(By.xpath("//button[contains(.,'Save as Draft')]")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		String message = driver.findElement(By.xpath("//span[@id='spanMessage']")).getText();
		return message;
	}
	public String generate_transmittal(WebDriver driver){
		driver.findElement(By.xpath("//button[contains(.,'Generate Transmittal')]")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		String message = driver.findElement(By.xpath("//span[@id='spanMessage']")).getText();
		return message;
	}
	public void check_transmittal_details(WebDriver driver, String trans_no, String hmo, Double amount, boolean set){
		
		driver.findElement(By.xpath("//input[@id='slcGuarantor']")).sendKeys(hmo);
		driver.findElement(By.xpath("//input[@id='txtTransmittalNo']")).sendKeys(trans_no);
		Select status = new Select(driver.findElement(By.xpath("//select[@id='slcStatus']")));
		status.selectByVisibleText("FINAL");
		driver.findElement(By.xpath("//button[contains(., 'Search')]")).click();
        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
        
        String cc = driver.findElement(By.xpath("//table[@id='data']/tbody/tr/td[1]")).getText();
        String tn = driver.findElement(By.xpath("//table[@id='data']/tbody/tr/td[2]")).getText();
        String td = driver.findElement(By.xpath("//table[@id='data']/tbody/tr/td[3]")).getText();
        String ad = driver.findElement(By.xpath("//table[@id='data']/tbody/tr/td[4]")).getText();
        String a = driver.findElement(By.xpath("//table[@id='data']/tbody/tr/td[5]")).getText();
        String s = driver.findElement(By.xpath("//table[@id='data']/tbody/tr/td[6]")).getText();
		
		if(cc.equals(hmo)){
			System.out.println("HMO is CORRECT");
		}else{
			System.out.println(cc+" is not equals to "+hmo);
		}
		if(tn.equals(trans_no)){
			System.out.println("Transmittal Number is CORRECT");
		}else{
			System.out.println(tn+" is not equals to "+trans_no);
		}
		if(hmo.contains("EASTWEST")){
			if(ad.equals("06/19/2014")){
				System.out.println("Availment Date is CORRECT");
			}else{
				System.out.println(ad+" is not equals to 06/19/2014");
			}
			if(set==true){
				Transmittal_ToWebApp.availment_date = "06/19/2014";
			}
		}else if(hmo.contains("COCOLIFE")){
			if(ad.equals("06/13/2014")){
				System.out.println("Availment Date is CORRECT");
			}else{
				System.out.println(ad+" is not equals to 06/13/2014");
			}
			if(set==true){
				Transmittal_ToWebApp.availment_date = "06/13/2014";
			}
		}
		if(s.equals("FINAL")){
			System.out.println("Status is CORRECT");
		}else{
			System.out.println(s+" is not equals to FINAL");
		}
		String currentDate = atl.date();
		if(td.equals(currentDate)){
			System.out.println("Transmission Date is CORRECT.");
			System.out.println("Transmission Date: "+td+ " vs "+ currentDate);
		}else{
			System.out.println(td+" is not equals to "+currentDate);
		}
		amount = Math.round(amount * 100.0) / 100.0;
		String myAmount = Double.toString(amount);
		a = a.replaceAll(",", "");
		if(myAmount.equals(a)){
			System.out.println("Transmission Amount is CORRECT");
		}else{
			System.out.println(a+" is not equals to "+myAmount);
		}
	}
	public String[] get_table_details_inpatient_pf(WebDriver driver, int x){
		
		String vn = driver.findElement(By.xpath("//div[@id='collapseOnes']/div/div/table/tbody/tr["+x+"]/td[1]")).getText();
		String ad = driver.findElement(By.xpath("//div[@id='collapseOnes']/div/div/table/tbody/tr["+x+"]/td[2]")).getText();
		String dd = driver.findElement(By.xpath("//div[@id='collapseOnes']/div/div/table/tbody/tr["+x+"]/td[3]")).getText();
		String soa = driver.findElement(By.xpath("//div[@id='collapseOnes']/div/div/table/tbody/tr["+x+"]/td[4]")).getText();
		String dn = driver.findElement(By.xpath("//div[@id='collapseOnes']/div/div/table/tbody/tr["+x+"]/td[5]")).getText();
		String pn = driver.findElement(By.xpath("//div[@id='collapseOnes']/div/div/table/tbody/tr["+x+"]/td[6]")).getText();
		String pfa = driver.findElement(By.xpath("//div[@id='collapseOnes']/div/div/table/tbody/tr["+x+"]/td[7]")).getText();

		String tableText[] = { vn, ad, dd, soa, dn, pn, pfa };
		
		return tableText;
		
	}
	public String[] get_table_details_outpatient_pf(WebDriver driver, int x){
		
		String vn = driver.findElement(By.xpath("//div[@id='collapseTwos']/div/div/table/tbody/tr["+x+"]/td[1]")).getText();
		String ad = driver.findElement(By.xpath("//div[@id='collapseTwos']/div/div/table/tbody/tr["+x+"]/td[2]")).getText();
		String soa = driver.findElement(By.xpath("//div[@id='collapseTwos']/div/div/table/tbody/tr["+x+"]/td[3]")).getText();
		String dn = driver.findElement(By.xpath("//div[@id='collapseTwos']/div/div/table/tbody/tr["+x+"]/td[4]")).getText();
		String pn = driver.findElement(By.xpath("//div[@id='collapseTwos']/div/div/table/tbody/tr["+x+"]/td[5]")).getText();
		String pfa = driver.findElement(By.xpath("//div[@id='collapseTwos']/div/div/table/tbody/tr["+x+"]/td[6]")).getText();

		String tableText[] = { vn, ad, soa, dn, pn, pfa };
		
		return tableText;
		
	}
	public void verify_hmo_final_transmittal(WebDriver driver){

		String trans_number = Transmittal_Details.transmittal_number;
		String transmittal_no = driver.findElement(By.xpath("//div[@id='editFormDiv']/span[3]")).getText();
		transmittal_no = transmittal_no.replaceAll(" ", "");
		if(trans_number.equals(transmittal_no)){
			System.out.println("Value of Transmittal Number: "+transmittal_no+" .CORRECT");
		}else{  System.out.println("Value of Transmittal Number: "+transmittal_no+" .INCORRECT"); }
	}
	public void verify_final_transmittal_date(WebDriver driver){
		//Verify if transmittal date is equals to current date
		String myDate = atl.date();
		String new_loa_date[] = myDate.split("/"); 
		
		String finalDate = new_loa_date[0]+"/"+new_loa_date[1]+"/"+new_loa_date[2];
		String transDate = driver.findElement(By.xpath("//div[@id='editFormDiv']/span[4]")).getText();
		
		if(finalDate.equals(transDate)){
			System.out.println("Transmittal Date is equals to current Date. CORRECT");
		}else{
			System.out.println("Transmittal Date is not equals to current Date. INCORRECT");
			System.out.println(finalDate);
			System.out.println(transDate);
		}
		
	}
	public boolean checkIt(String text){
		if(text.contains("Nothing found to display")){
			return true;
		}else{
			return false;
		}
			
	}
	public void generate_report_not_transmitted(WebDriver driver, String hmo) throws InterruptedException{
		driver.findElement(By.xpath("//input[@id='slcGuarantor']")).sendKeys(hmo);
		driver.findElement(By.xpath("//html/body/ul/li/a")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		Select status = new Select(driver.findElement(By.xpath("//select[@id='slcNotIncluded']")));
		status.selectByVisibleText("Visit Not Transmitted");
        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
        Thread.sleep(7000);
	}
	
	
}
